package com.dd.service.impl;

import com.dd.mapper.DeliveryMapper;
import com.dd.mapper.OrderMapper;
import com.dd.model.Delivery;
import com.dd.model.Order;
import com.dd.service.DeliveryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 配送服务实现类
 */
@Service
public class DeliveryServiceImpl implements DeliveryService {
    
    @Autowired
    private DeliveryMapper deliveryMapper;
    
    @Autowired
    private OrderMapper orderMapper;
    
    @Override
    public List<Delivery> findAllDeliveries() {
        return deliveryMapper.findAll();
    }
    
    @Override
    public Delivery getDeliveryById(Integer deliveryId) {
        return deliveryMapper.findById(deliveryId);
    }
    
    @Override
    public Delivery getDeliveryByOrderId(Integer orderId) {
        return deliveryMapper.findByOrderId(orderId);
    }
    
    @Override
    public List<Delivery> findDeliveriesByStatus(String status) {
        return deliveryMapper.findByStatus(status);
    }
    
    @Override
    @Transactional
    public boolean addDelivery(Delivery delivery) {
        // 检查订单是否存在
        Order order = orderMapper.findBasicById(delivery.getOrderId());
        if (order == null) {
            return false;
        }
        
        // 设置默认状态
        if (delivery.getStatus() == null || delivery.getStatus().isEmpty()) {
            delivery.setStatus("processing");
        }
        
        // 添加配送信息
        int result = deliveryMapper.insert(delivery);
        
        // 更新订单状态为已发货
        if (result > 0 && "processing".equals(delivery.getStatus()) == false) {
            orderMapper.updateStatus(order.getOrderId(), "shipped");
        }
        
        return result > 0;
    }
    
    @Override
    @Transactional
    public boolean updateDelivery(Delivery delivery) {
        return deliveryMapper.update(delivery) > 0;
    }
    
    @Override
    @Transactional
    public boolean updateDeliveryStatus(Integer deliveryId, String status) {
        // 获取配送信息
        Delivery delivery = deliveryMapper.findById(deliveryId);
        if (delivery == null) {
            return false;
        }
        
        // 更新配送状态
        int result = deliveryMapper.updateStatus(deliveryId, status);
        
        // 如果配送已完成，更新订单状态为已送达
        if (result > 0 && "delivered".equals(status)) {
            orderMapper.updateStatus(delivery.getOrderId(), "delivered");
        }
        
        return result > 0;
    }
    
    @Override
    @Transactional
    public boolean deleteDelivery(Integer deliveryId) {
        return deliveryMapper.deleteById(deliveryId) > 0;
    }

    @Override
    public boolean deleteDeliveryByOrderId(Integer orderId) {
        return deliveryMapper.deleteDeliveryByOrderId(orderId);
    }
} 